package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {





    @Autowired
    private TrackInfoService trackInfoService;

    /**
     * 批量获取下单付费声音列表
     * @param trackId
     * @param trackCount
     * @return
     */
    @GetMapping("api/album/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable("trackId") Long trackId, @PathVariable("trackCount") Integer trackCount){
        List<TrackInfo> list=trackInfoService.findPaidTrackInfoList(trackId,trackCount);
        return Result.ok(list);
    }

    @TsLogin

    @Operation(summary = "获取用户声音分集购买支付列表")
    @GetMapping("/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String,Object>>> findUserTrackPaidList(@PathVariable Long trackId) {

        List<Map<String,Object>> list = trackInfoService.findUserTrackPaidList(trackId);
        return Result.ok(list);
    }



    @Operation(summary = "根据专辑id查询声音列表")
    @GetMapping("/findAlbumTrackPage/{albumId}/{pageNum}/{pageSize}")
    public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId,@PathVariable Long pageNum, @PathVariable Long pageSize) {
        Page<AlbumTrackListVo> page = new Page<>(pageNum, pageSize);
        Long userId = AuthContextHolder.getUserId();
        IPage<AlbumTrackListVo> iPage = trackInfoService.findAlbumTrackPage(page, albumId,userId);
        return Result.ok(iPage);
    }

    //上传声音localhost/api/album/trackInfo/uploadTrack
    @Operation(summary = "上传声音")
    @PostMapping("/uploadTrack")
    public Result uploadTrack(MultipartFile file) {
        Map<String, Object> map = trackInfoService.uploadTrack(file);
        return Result.ok(map);
    }

    //api/album/trackInfo/saveTrackInfo
    @Operation(summary = "保存声音信息")
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }

    @Operation(summary = "查询声音列表")
    @PostMapping("/findUserTrackPage/{page}/{limit}")
    public Result findUserTrackPage(@PathVariable Long page, @PathVariable Long limit, @RequestBody TrackInfoQuery trackInfoQuery) {
        Page<Object> page1 = new Page<>(page, limit);
        Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        IPage<TrackListVo> iPage = trackInfoService.findUserTrackPage(page1, trackInfoQuery);
        return Result.ok(iPage);
    }

    /**
     *  删除声音信息
     * @param id
     * @return
     */
    @Operation(summary = "删除声音信息")
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable Long trackId) {
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }
    @Operation(summary = "根据id查询声音信息")
    @GetMapping("/getTrackInfo/{trackId}")
    public Result getTrackInfo(@PathVariable Long trackId) {
        TrackInfo trackInfo = trackInfoService.getTrackInfoById(trackId);

        return Result.ok(trackInfo);
    }
    @Operation(summary = "修改声音信息")
    @PutMapping("/updateTrackInfo/{trackId}")
    public Result updateTrackInfo(@PathVariable Long trackId,@RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(trackInfoVo,trackId);
        return Result.ok();
    }


}

